const path = require("path");
const fs = require("fs-extra");

const inquirer = require("inquirer");
const chalk = require("chalk");

const Creator = require("./Creator");
module.exports = async function (projectName, options) {
  //   console.log(projectName, options);
  //创建项目
  const cwd = process.cwd(); //获取当前命令执行的工作目录
  //   console.log(cwd);
  const targetDir = path.join(cwd, projectName);
  if (fs.existsSync(targetDir)) {
    if (options.force) {
      //强制创建 删除已有的
      await fs.remove(targetDir);
    } else {
      //提示用户是否确定要覆盖
      let { action } = await inquirer.prompt([
        {
          name: "action",
          type: "list", //类型很丰富
          message: "Target directory already exists, pick an action",
          choices: [
            { name: "overwrite", value: "overwrite" },
            { name: "cancel", value: false },
          ],
        },
      ]);
      console.log(action);
      if (!action) {
        return;
      } else if (action == "overwrite") {
        console.log(`\r\n${chalk.yellow("Removing...")}\r\n`);
        await fs.remove(targetDir);
      }
    }
  }
  //创建项目
  const creator = new Creator(projectName, targetDir);
  creator.create();
};
